package com.cgs.query.client.remote

import com.cgs.query.exception.QueryException
import com.cgs.query.handler.RemoteHandler
import com.cgs.query.service.IQueryService
import java.lang.reflect.Proxy

class RemoteServiceFactory(private val remoteHandler: RemoteHandler) {

    fun getQueryService() = getProxyService(IQueryService::class.java, remoteHandler)

    private fun <T> getProxyService(clz: Class<T>, remoteHandler: RemoteHandler): T {
        try {
            return Proxy.newProxyInstance(this.javaClass.classLoader, arrayOf(clz), RemoteInvocationHandler(remoteHandler)) as T
        } catch (e: Exception) {
            throw QueryException("获取$clz 代理失败", e)
        }
    }
}